﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.SalesAndCustomerRelations
{
    public class CustomerSalesInfos : DetailView<customersalesinfo>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<customersalesinfo> EntityList;
        private new customersalesinfo Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<customersalesinfo>

        public override List<customersalesinfo> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.customersalesinfoes.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<customersalesinfo> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.customersalesinfoes.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override customersalesinfo GetEntity()
        {
            _context = new project5188Entities();
            Entity = new customersalesinfo();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTocustomersalesinfoes(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<customersalesinfo> GetEntityListByParameters(long customerTypeId, string companyNoText, long serviceTypeId, string companyName)
        {
            _context = new project5188Entities();

            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            dynamicString += " And it.customerinfo.IsDeleted = @CustomerIsDeleted";
            parameterList.Add(new ObjectParameter("CustomerIsDeleted", false));

            if (customerTypeId > 0)
            {
                dynamicString += " And it.customerinfo.c_CustomerTypeParameterValueID_20 = @CustomerTypeId";
                parameterList.Add(new ObjectParameter("CustomerTypeId", customerTypeId));
            }

            if (!String.IsNullOrEmpty(companyNoText) && !String.IsNullOrWhiteSpace(companyNoText))
            {
                dynamicString += " And it.customerinfo.d_CompanyNo = @CompanyNo";
                parameterList.Add(new ObjectParameter("CompanyNo", Convert.ToInt32(companyNoText)));
            }

            if (serviceTypeId > 0)
            {
                dynamicString += " And it.customerinfo.o_ServiceTypeParameterValueID_23 = @ServiceTypeId";
                parameterList.Add(new ObjectParameter("ServiceTypeId", serviceTypeId));
            }

            if (!string.IsNullOrEmpty(companyName) && !string.IsNullOrWhiteSpace(companyName))
            {
                dynamicString += " And it.customerinfo.b_CompanyName = @CompanyName";
                parameterList.Add(new ObjectParameter("CompanyName", companyName));
            }

            EntityList =
                _context.customersalesinfoes.Where(dynamicString, parameterList.ToArray()).
                    OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }
    }
}
